package gw.com.android.greendao;

import com.gwtsz.android.rxbus.RxBus;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

import gw.com.android.app.GTConfig;
import gw.com.android.ui.bulletin.NotifyData;
import www.com.library.app.Logger;

/**
 * @author jett
 * @since 2018-07-31.
 */
public class NotifyDataHelper {

    public static void insertOrReplace(List<NotifyData> entities) {
        DaoHelper.getInstance().getNotifyDataDao().insertOrReplaceInTx(entities);
    }

    public static boolean exsit(long id) {
        return DaoHelper.getInstance().getNotifyDataDao().queryBuilder().where(NotifyDataDao.Properties._id.eq(id)).count() > 0;
    }

    public static int getUnreadCount() {
        return (int) DaoHelper.getInstance().getNotifyDataDao().queryBuilder()
                .where(NotifyDataDao.Properties.IsReaded.eq(false)).count();
    }

    public static int getUnreadCount(int type) {
        return (int) DaoHelper.getInstance().getNotifyDataDao().queryBuilder()
                .where(NotifyDataDao.Properties.TypeParentId.eq(type), NotifyDataDao.Properties.IsReaded.eq(false)).count();
    }

    public static NotifyData getLastNotice(int type) {
        return DaoHelper.getInstance().getNotifyDataDao().queryBuilder()
                .where(NotifyDataDao.Properties.TypeParentId.eq(type))
                .orderDesc(NotifyDataDao.Properties.Ctime, NotifyDataDao.Properties._id)
                .limit(1).build().unique();
    }

    public static List<NotifyData> getNotify(int type) {
        return DaoHelper.getInstance().getNotifyDataDao().queryBuilder()
                .where(NotifyDataDao.Properties.TypeParentId.eq(type))
                .orderDesc(NotifyDataDao.Properties.Ctime, NotifyDataDao.Properties._id).list();
    }

    public static void setAllNoticeReaded() {
        List<NotifyData> list = DaoHelper.getInstance().getNotifyDataDao().queryBuilder().where(NotifyDataDao.Properties.IsReaded.eq(false)).list();
        if (list != null && !list.isEmpty()) {
            for (NotifyData notice : list) {
                notice.setIsReaded(true);
            }
            DaoHelper.getInstance().getNotifyDataDao().updateInTx(list);
        }
    }

    public static void setAllNoticeReaded(int type) {
        QueryBuilder<NotifyData> queryBuilder = DaoHelper.getInstance().getNotifyDataDao().queryBuilder();
        queryBuilder.where(NotifyDataDao.Properties.IsReaded.eq(false));
        queryBuilder.where(NotifyDataDao.Properties.TypeParentId.eq(type));
        List<NotifyData> list = queryBuilder.list();
        if (list != null && !list.isEmpty()) {
            for (NotifyData notice : list) {
                notice.setIsReaded(true);
            }
            DaoHelper.getInstance().getNotifyDataDao().updateInTx(list);
        }
    }

    public static boolean setNotifyReaded(long id) {
        NotifyData unique = DaoHelper.getInstance().getNotifyDataDao().queryBuilder()
                .where(NotifyDataDao.Properties._id.eq(id))
                .unique();
        if (unique != null) {
            if (unique.isBurn) {
                DaoHelper.getInstance().getNotifyDataDao().delete(unique);
                return true;
            }
            if (!unique.isReaded) {
                unique.isReaded = true;
                DaoHelper.getInstance().getNotifyDataDao().update(unique);
                return true;
            }
        }
        return false;
    }

    public static void deleteExpireData() {
        boolean hasChange = false;
        // 删除超过30天的消息
        long timeStamp = GTConfig.instance().getLongValue("push_time_stamp_" + GTConfig.instance().mCurName, -1);
        if (timeStamp > 0) {
            long expireTime = timeStamp - 30 * 24 * 60 * 60;
            List<NotifyData> list = DaoHelper.getInstance().getNotifyDataDao().queryBuilder()
                    .where(NotifyDataDao.Properties.Ctime.le(expireTime)).list();
            if (list != null && !list.isEmpty()) {
                List<Long> keys = new ArrayList<>();
                for (NotifyData data : list) {
                    keys.add(data._id);
                }
                DaoHelper.getInstance().getNotifyDataDao().deleteByKeyInTx(keys);
                if (keys.size() > 0) {
                    Logger.i("_notify", "delete expire notify.11 size = " + keys.size());
                    hasChange = true;
                }
            }
        }
        // 删除失效的消息
        long currentTimeMillis = System.currentTimeMillis() / 1000;
        StringBuilder where = new StringBuilder();
        where.append(currentTimeMillis)
                .append(" - ").append(NotifyDataDao.Properties.CreateTime.columnName)
                .append(" >= ").append(NotifyDataDao.Properties.DisplaySecond.columnName);
        List<NotifyData> list = DaoHelper.getInstance().getNotifyDataDao().queryBuilder()
                .where(NotifyDataDao.Properties.DisplaySecond.gt(0), new WhereCondition.StringCondition(where.toString())).list();
        if (list != null && !list.isEmpty()) {
            List<Long> keys = new ArrayList<>();
            for (NotifyData data : list) {
                keys.add(data._id);
            }
            DaoHelper.getInstance().getNotifyDataDao().deleteByKeyInTx(keys);
            if (keys.size() > 0) {
                Logger.i("_notify", "delete expire notify.22 size = " + keys.size());
                hasChange = true;
            }
        }
        if (hasChange) {
            notifyChange();
        }
    }

    public static void notifyChange() {
        RxBus.getInstance().post("notification_data_update", true);
    }

}
